﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Models;
using SD.IOC.Core.Mediators;
using SlamDunk.MES.Client.ViewModels.Facility;
using SlamDunk.MES.IAppService.DTOs.Outputs.MaintenanceContext;
using SlamDunk.MES.IAppService.Interfaces;
using System;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using System.Windows;

namespace SlamDunk.MES.Client.ViewModels.EquipmentMaintenance
{
    /// <summary>
    /// 设备维护记录修改视图模型
    /// </summary>
    public class UpdateViewModel : ScreenBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 维护管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IMaintenanceContract> _maintenanceContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public UpdateViewModel(ServiceProxy<IMaintenanceContract> maintenanceContract, IWindowManager windowManager)
        {
            this._maintenanceContract = maintenanceContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region # 属性

        #region 设备维护记录Id —— Guid EquipmentMaintenanceId
        /// <summary>
        /// 设备维护记录Id
        /// </summary>
        public Guid EquipmentMaintenanceId { get; set; }
        #endregion

        #region 设备维护记录编号 —— string EquipmentMaintenanceNo
        /// <summary>
        /// 设备维护记录编号
        /// </summary>
        public string EquipmentMaintenanceNo { get; set; }
        #endregion

        #region 设备维护记录名称 —— string EquipmentMaintenanceName
        /// <summary>
        /// 设备维护记录名称
        /// </summary>
        public string EquipmentMaintenanceName { get; set; }
        #endregion

        #region 设备维护类名称 —— string EquipmentMaintainClassName
        /// <summary>
        /// 设备维护类名称
        /// </summary>
        public string EquipmentMaintainClassName { get; set; }
        #endregion

        #region 设备资产名称 —— string EquipmentAssetName
        /// <summary>
        /// 设备资产名称
        /// </summary>
        public string EquipmentAssetName { get; set; }
        #endregion

        #region 维护日期 —— DateTime? MaintainedDate
        /// <summary>
        /// 维护日期
        /// </summary>
        [DependencyProperty]
        public DateTime? MaintainedDate { get; set; }
        #endregion

        #region 维护工时 —— decimal? MaintainedHour
        /// <summary>
        /// 维护工时
        /// </summary>
        [DependencyProperty]
        public decimal? MaintainedHour { get; set; }
        #endregion

        #region 维护部位 —— string MaintainedParts
        /// <summary>
        /// 维护部位
        /// </summary>
        [DependencyProperty]
        public string MaintainedParts { get; set; }
        #endregion

        #region 维护内容 —— string MaintainedContent
        /// <summary>
        /// 维护内容
        /// </summary>
        [DependencyProperty]
        public string MaintainedContent { get; set; }
        #endregion

        #region 备件使用 —— string SparePartsUse
        /// <summary>
        /// 备件使用
        /// </summary>
        [DependencyProperty]
        public string SparePartsUse { get; set; }
        #endregion

        #region 维护人 —— string Maintainer
        /// <summary>
        /// 维护人
        /// </summary>
        [DependencyProperty]
        public string Maintainer { get; set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        [DependencyProperty]
        public string Description { get; set; }
        #endregion

        #region 已选工厂模型 —— Node SelectedFacility
        /// <summary>
        /// 已选工厂模型
        /// </summary>
        [DependencyProperty]
        public Node SelectedFacility { get; set; }
        #endregion

        #endregion

        #region # 方法

        //Initializations

        #region 加载 —— void Load(EquipmentMaintenanceInfo equipmentMaintenance)
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="equipmentMaintenance">设备维护记录</param>
        public void Load(EquipmentMaintenanceInfo equipmentMaintenance)
        {
            this.EquipmentMaintenanceId = equipmentMaintenance.Id;
            this.EquipmentMaintenanceNo = equipmentMaintenance.Number;
            this.EquipmentMaintenanceName = equipmentMaintenance.Name;
            this.EquipmentMaintainClassName = equipmentMaintenance.EquipmentMaintainClassInfo.Name;
            this.EquipmentAssetName = equipmentMaintenance.EquipmentAssetInfo.Name;
            this.MaintainedDate = equipmentMaintenance.MaintainedDate;
            this.MaintainedHour = equipmentMaintenance.MaintainedHour;
            this.MaintainedParts = equipmentMaintenance.MaintainedParts;
            this.MaintainedContent = equipmentMaintenance.MaintainedContent;
            this.SparePartsUse = equipmentMaintenance.SparePartsUse;
            this.Maintainer = equipmentMaintenance.Maintainer;
            this.Description = equipmentMaintenance.Description;
            this.SelectedFacility = equipmentMaintenance.FacilityInfo == null
                ? null
                : new Node(equipmentMaintenance.FacilityInfo.Id, equipmentMaintenance.FacilityInfo.Number, equipmentMaintenance.FacilityInfo.Name, null, null);
        }
        #endregion


        //Actions

        #region 选择工厂模型 —— async void SelectFacility()
        /// <summary>
        /// 选择工厂模型
        /// </summary>
        public async void SelectFacility()
        {
            this.Busy();

            SelectFacilityViewModel viewModel = ResolveMediator.Resolve<SelectFacilityViewModel>();
            bool? result = await this._windowManager.ShowDialogAsync(viewModel);
            if (result == true)
            {
                this.SelectedFacility = viewModel.SelectedFacility;
            }

            this.Idle();
        }
        #endregion

        #region 清空工厂模型 —— async void ClearFacility()
        /// <summary>
        /// 清空工厂模型
        /// </summary>
        public async void ClearFacility()
        {
            this.SelectedFacility = null;
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            if (string.IsNullOrWhiteSpace(this.EquipmentMaintenanceName))
            {
                MessageBox.Show("维护记录名称不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            if (this.MaintainedDate == null)
            {
                MessageBox.Show("维护日期不可为空！", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            #endregion

            this.Busy();

            await Task.Run(() => this._maintenanceContract.Channel.UpdateEquipmentMaintenance(this.EquipmentMaintenanceId, this.MaintainedDate.Value, this.MaintainedHour, this.MaintainedParts, this.MaintainedContent, this.SparePartsUse, this.Maintainer, this.SelectedFacility?.Id, this.Description));

            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #endregion
    }
}
